﻿Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend Class SecondaryViewProvider
        ' Methods
        Friend Shared Function FindActivity(ByVal designer As StructuredCompositeActivityDesigner, ByVal activityType As Type) As Activity
            Dim activity As CompositeActivity = [TryCast](designer.Activity,CompositeActivity)
            If ((Not activityType Is Nothing) AndAlso (Not activity Is Nothing)) Then
                Dim activity2 As Activity
                For Each activity2 In activity.Activities
                    If activityType.IsAssignableFrom(activity2.GetType) Then
                        Return activity2
                    End If
                Next
            End If
            Return Nothing
        End Function

        Friend Shared Function GetActivityTypes(ByVal designer As StructuredCompositeActivityDesigner) As IList([Of] Type)
            Dim list As New List([Of] Type)
            Dim views As ReadOnlyCollection([Of] DesignerView) = designer.Views
            Dim i As Integer
            For i = 1 To views.Count - 1
                Dim item As Type = [TryCast](views.Item(i).UserData.Item(SecondaryView.UserDataKey_ActivityType),Type)
                list.Add(item)
            Next i
            Return list.AsReadOnly
        End Function

        Friend Shared Function GetViews(ByVal designer As StructuredCompositeActivityDesigner) As ReadOnlyCollection([Of] DesignerView)
            If (designer.Activity Is Nothing) Then
                Throw New ArgumentException("Component can not be null!")
            End If
            Dim flag As Boolean = Not designer.IsEditable
            Dim list As New List([Of] Object())
            Dim toolboxDisplayName As String = ActivityToolboxItem.GetToolboxDisplayName(designer.Activity.GetType)
            list.Add(New Object() { designer.Activity.GetType, DR.GetString("ViewActivity", New Object() { toolboxDisplayName }) })
            If (Not designer.Activity.Site Is Nothing) Then
                Dim service As WorkflowDesignerLoader = [TryCast](designer.Activity.Site.GetService(GetType(WorkflowDesignerLoader)),WorkflowDesignerLoader)
                Dim c As Type = designer.Activity.GetType
                If ((service Is Nothing) OrElse (GetType(CompositeActivity).IsAssignableFrom(c) AndAlso (Not flag OrElse (Not SecondaryViewProvider.FindActivity(designer, GetType(CancellationHandlerActivity)) Is Nothing)))) Then
                    list.Add(New Object() { GetType(CancellationHandlerActivity), DR.GetString("ViewCancelHandler", New Object(0  - 1) {}) })
                End If
                If ((service Is Nothing) OrElse (GetType(CompositeActivity).IsAssignableFrom(c) AndAlso (Not flag OrElse (Not SecondaryViewProvider.FindActivity(designer, GetType(FaultHandlersActivity)) Is Nothing)))) Then
                    list.Add(New Object() { GetType(FaultHandlersActivity), DR.GetString("ViewExceptions", New Object(0  - 1) {}) })
                End If
                If ((service Is Nothing) OrElse ((TypeOf designer.Activity Is ICompensatableActivity AndAlso GetType(CompositeActivity).IsAssignableFrom(c)) AndAlso (Not flag OrElse (Not SecondaryViewProvider.FindActivity(designer, GetType(CompensationHandlerActivity)) Is Nothing)))) Then
                    list.Add(New Object() { GetType(CompensationHandlerActivity), DR.GetString("ViewCompensation", New Object(0  - 1) {}) })
                End If
                If ((service Is Nothing) OrElse (Type.GetType("System.Workflow.Activities.EventHandlingScopeActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35").IsAssignableFrom(c) AndAlso (Not flag OrElse (Not SecondaryViewProvider.FindActivity(designer, Type.GetType("System.Workflow.Activities.EventHandlersActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")) Is Nothing)))) Then
                    list.Add(New Object() { Type.GetType("System.Workflow.Activities.EventHandlersActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"), DR.GetString("ViewEvents", New Object(0  - 1) {}) })
                End If
            End If
            Dim list2 As New List([Of] DesignerView)
            Dim i As Integer
            For i = 0 To list.Count - 1
                Dim activityType As Type = [TryCast](list.Item(i)(0),Type)
                Dim item As DesignerView = New SecondaryView(designer, (i + 1), [TryCast](list.Item(i)(1),String), activityType)
                list2.Add(item)
            Next i
            Return list2.AsReadOnly
        End Function

        Friend Shared Sub OnViewRemoved(ByVal designer As StructuredCompositeActivityDesigner, ByVal viewTypeRemoved As Type)
            Dim views As ReadOnlyCollection([Of] DesignerView) = designer.Views
            Dim i As Integer
            For i = 1 To views.Count - 1
                Dim type As Type = [TryCast](views.Item(i).UserData.Item(SecondaryView.UserDataKey_ActivityType),Type)
                If (viewTypeRemoved Is type) Then
                    views.Item(i).UserData.Item(SecondaryView.UserDataKey_Designer) = Nothing
                End If
            Next i
        End Sub


        ' Fields
        Private Const EventHandlersRef As String = "System.Workflow.Activities.EventHandlersActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Private Const EventHandlingScopeRef As String = "System.Workflow.Activities.EventHandlingScopeActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    End Class
End Namespace

